import {
    getAction,
    postAction,
    downFilePost,
    postFormAction3,
} from '@/api/common'

let errors = {
    title: '提示',
    content: '删除失败！',
    icon: 'error',
}
let success = {
    title: '提示',
    content: '删除成功！',
    icon: 'success',
}
export default {
    data() {
        return {
            query: {},
            loading: false,
            selectTable: [],
            renderedContent: {},
        }
    },
    mounted() {
        // this.getList()
    },
    methods: {
        initData() {},
        getList(toFirst) {
            this.loading = true
            if (toFirst) {
                this.query.page = 1
            }
            if (this.urlMap.list) {
                getAction(this.urlMap.list, this.query)
                    .then((res) => {
                        if (res.flag) {
                            this.renderedContent.tableData = res ? res.data : []
                            this.initData(res)
                        } else {
                            this.$message.error(res.data)
                        }
                        this.loading = false
                    })
                    .catch((error) => {
                        this.loading = false
                        this.$message.error('服务器错误，请稍后重试！')
                    })
            } else {
                this.initData()
            }
        },
        reset() {
            if (this.$refs.table) {
                this.$refs.table.page = 1
            }
            this.query = {}
            this.timeValue = []
            this.getList()
        },
        // 点击搜索按钮
        searchData(val) {
            this.getList()
        },
        // 修改页码请求数据
        changeGetList(data) {
            this.query.page = data.page
            this.query.limit = data.limit
            this.getList()
        },
        handleSelect(row) {
            this.activeIndex = row
        },
        // 操作--add、edit、show
        handleDialog(title, row) {
            this.$refs.dialog.showDialog(title, row)
        },
        // 操作--删除
        handleDelete(row) {
            this.msgContent = {
                title: '询问',
                content: '确定要删除该项？',
                icon: 'question',
            }
            this.$refs.msgBox.initData(this.msgContent, row)
        },
        deleteRow(row) {
            if (!this.urlMap.delete) {
                this.$message.warning('请添加删除链接！')
                return
            }
            this.$refs.msgBox.cancel()
            this.loading = true
            postAction(this.urlMap.delete, { id: row.id })
                .then((res) => {
                    if (res.flag) {
                        this.$message.success(res.data)
                        this.getList()
                    } else {
                        this.$message.error('删除失败！' + res.data)
                    }
                    this.loading = false
                })
                .catch((error) => {
                    this.$message.error('服务器错误，请稍后重试！')
                })
        },
        // 批量删除
        deleteList() {
            if (!this.selectTable.length) {
                this.$message.warning('请至少选择一条记录！')
                return
            } else {
                const idsArr = this.selectTable.map((el) => {
                    return el.id
                })
                this.msgContent = {
                    title: '询问',
                    content: '确定删除该项？',
                    icon: 'question',
                }
                this.$refs.msgBox.initData(
                    this.msgContent,
                    idsArr,
                    'handleMultiDelete'
                )
            }
        },
        handleMultiDelete(row) {
            postFormAction3(this.urlMap.deleteMulti || this.urlMap.delete, {
                id: row.toString(),
            })
                .then((res) => {
                    if (res.flag) {
                        this.$message.success(res.message)
                        this.selectTable = []
                        this.$refs.table.clearSelection()
                        this.getList()
                    } else {
                        this.$message.error('删除失败！' + res.message)
                    }
                    this.$refs.msgBox.cancel()
                    this.loading = false
                })
                .catch((error) => {
                    this.$message.error('服务器错误，请稍后重试！')
                })
        },

        // 下载excel
        handleExcelDialog() {
            if (!this.urlMap.exportExcel) {
                this.$message.warning('请添加导出链接！')
                return
            } else {
                this.msgContent = {
                    title: '询问',
                    content: '确定导出Excel？',
                    icon: 'question',
                }
                this.$refs.msgBox.initData(
                    this.msgContent,
                    {},
                    'handleDownloadXls'
                )
            }
        },
        handleDownloadXls(fileName) {
            let query = this.query
            this.downloadLoading = true
            // downFilePost(this.urlMap.exportExcel, query).then(({ data }) => {
            //   if (!data) {
            //     this.$message.warning('文件下载失败')
            //     return
            //   }
            // if (typeof window.navigator.msSaveBlob !== 'undefined') {
            //   window.navigator.msSaveBlob(
            //     new Blob([data], { type: 'application/vnd.ms-excel' }),
            //     fileName + '.xls'
            //   )
            // } else {
            //   let url = window.URL.createObjectURL(
            //     new Blob([data], { type: 'application/vnd.ms-excel' })
            //   )
            //   let link = document.createElement('a')
            //   link.style.display = 'none'
            //   link.href = url
            //   link.setAttribute('download', fileName + '.xls')
            //   document.body.appendChild(link)
            //   link.click()
            //   document.body.removeChild(link) //下载完成移除元素
            //   window.URL.revokeObjectURL(url) //释放掉blob对象
            // }
            // this.downloadLoading = false

            postAction(this.urlMap.exportExcel, query).then((res) => {
                if (!res.flag) {
                    this.$message.warning('文件下载失败')
                    return
                } else {
                    this.$message.success(res.message)
                    getAction(res.data).then(({ data }) => {
                        if (
                            typeof window.navigator.msSaveBlob !== 'undefined'
                        ) {
                            window.navigator.msSaveBlob(
                                new Blob([data], {
                                    type: 'application/vnd.ms-excel',
                                }),
                                fileName + '.xls'
                            )
                        } else {
                            let url = window.URL.createObjectURL(
                                new Blob([data], {
                                    type: 'application/vnd.ms-excel',
                                })
                            )
                            let link = document.createElement('a')
                            link.style.display = 'none'
                            link.href = url
                            link.setAttribute('download', fileName + '.xls')
                            document.body.appendChild(link)
                            link.click()
                            document.body.removeChild(link) //下载完成移除元素
                            window.URL.revokeObjectURL(url) //释放掉blob对象
                        }
                    })
                }

                this.downloadLoading = false
            })
        },
        // 查看图表
        handleChartDialog() {
            if (!this.query.asyncID) {
                this.$message.error('工程不能为空！')
                return
            }
            if (!this.hasData) {
                this.$message.error('请先查询或该条件下无数据，不能导出')
                return
            }
            let title = this.projectList.find(
                (item) => item.value == this.query.asyncID
            ).label
            this.$refs.chartDialog.init(this.query, title)
        },
        // 展示折线图
        showLineDialog(data) {
            let x = []
            let y = []
            if (data.list.length) {
                data.list.forEach((item) => {
                    x.push(
                        this.$options.filters['dateFormat'](
                            item.dateTime['time']
                        )
                    )
                    y.push(item.diffValue)
                })
            } else {
                return this.$message.warning('暂无数据')
            }
            this.$refs.lineDialog.init([x, y], data.object)
        },
    },
}
